﻿using System;
using System.Linq;
using System.Web.Mvc;
using Ctef.Web.Interfaces;
using Ctef.Web.Models;
using Ctef.Web.Repositories;
using Ctef.Web.Resources.Involve;

namespace Ctef.Web.Controllers
{
    public class InvolveController : Controller
    {
        private readonly IEmailRepository emailRepository;
        private readonly IProductRepository productRepository;

        public InvolveController() : this (new EmailRepository(), new ProductRepository())
        { }

        public InvolveController(IEmailRepository emailRep, IProductRepository prodRep)
        {
            if (emailRep == null)
            {
                throw new ArgumentNullException("emailRep");
            }

            if (prodRep == null)
            {
                throw new ArgumentNullException("prodRep");
            }

            emailRepository = emailRep;
            productRepository = prodRep;
        }

        [HttpGet]
        public ActionResult Index()
        {
            return RedirectToAction("Donate");
        }

        [HttpGet]
        public ActionResult Donate()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Volunteer()
        {
            return View(new VolunteerInfoViewModel());
        }

        [HttpGet]
        public ActionResult CardContest(int? id)
        {
            var pageId = id.HasValue ? id.Value : 1;
            if (pageId < 1) pageId = 1;

            ViewData["CurPage"] = pageId;

            return View();
        }

        [HttpPost]
        public JsonResult RegisterVolunteer(VolunteerInfoViewModel model)
        {
            var obj = emailRepository.SendEmail(model) ?
                new { message = Strings.Volunteer_ConfirmRegistration, success = 1 } :
                new { message = Strings.Volunteer_ErrorRegistration, success = 0 };

            return Json(obj);
        }

        [HttpPost]
        public JsonResult PledgeNow(PledgeViewModel model)
        {
            var obj = emailRepository.SendEmail(model) ?
                new { message = Strings.Volunteer_ConfirmRegistration, success = 1 } :
                new { message = Strings.Volunteer_ErrorRegistration, success = 0 };

            return Json(obj);
        }

        [HttpGet]
        public JsonResult GetCards(int? id)
        {
            const int ScaleFactor = 4;
            const int CardsPerPage = 25;

            IPaging paging = new Paging { 
                CurrentPage = id.HasValue ? id.Value : 1,
                ItemCountPerPage = CardsPerPage
            };

            var cards = productRepository.GetHolidayCards(paging)
                .Select(c => new { url = c.ImageUrl, width = c.Width / ScaleFactor, height = c.Height / ScaleFactor, id = c.ItemNumber, buttonid = c.ButtonId });

            var result = new { current = paging.CurrentPage, total = paging.TotalPageCount, cards = cards };

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        protected override void HandleUnknownAction(string actionName)
        {
            this.View("Donate").ExecuteResult(this.ControllerContext);
        }
    }
}
